package org.example.rzfx.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.rzfx.dto.Result;
import org.example.rzfx.entity.OpLog;
import org.example.rzfx.mapper.OpLogMapper;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 操作日志控制器
 */
@RestController
@RequestMapping("/api/op-log")
@CrossOrigin
public class OpLogController {
    
    @Resource
    private OpLogMapper opLogMapper;
    
    /**
     * 分页查询操作日志（管理员权限）
     */
    @GetMapping("/page")
    @SaCheckRole("1")
    public Result<?> pageOpLogs(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "20") Integer pageSize,
            @RequestParam(required = false) Long userId,
            @RequestParam(required = false) String opType,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        
        LambdaQueryWrapper<OpLog> wrapper = new LambdaQueryWrapper<>();
        
        if (userId != null) {
            wrapper.eq(OpLog::getUserId, userId);
        }
        if (opType != null && !opType.isEmpty()) {
            wrapper.eq(OpLog::getOpType, opType);
        }
        if (startTime != null) {
            wrapper.ge(OpLog::getOpTime, startTime);
        }
        if (endTime != null) {
            wrapper.le(OpLog::getOpTime, endTime);
        }
        
        wrapper.orderByDesc(OpLog::getOpTime);
        
        Page<OpLog> page = new Page<>(pageNum, pageSize);
        return Result.success(opLogMapper.selectPage(page, wrapper));
    }
    
    /**
     * 查询当前用户操作日志
     */
    @GetMapping("/my-logs")
    public Result<?> getMyLogs(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "20") Integer pageSize) {
        
        Long userId = cn.dev33.satoken.stp.StpUtil.getLoginIdAsLong();
        
        LambdaQueryWrapper<OpLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OpLog::getUserId, userId)
               .orderByDesc(OpLog::getOpTime);
        
        Page<OpLog> page = new Page<>(pageNum, pageSize);
        return Result.success(opLogMapper.selectPage(page, wrapper));
    }
    
    /**
     * 获取操作日志详情
     */
    @GetMapping("/{id}")
    @SaCheckRole("1")
    public Result<OpLog> getOpLogDetail(@PathVariable Long id) {
        OpLog opLog = opLogMapper.selectById(id);
        if (opLog == null) {
            return Result.error("操作日志不存在");
        }
        return Result.success(opLog);
    }
    
    /**
     * 统计操作类型分布（管理员权限）
     */
    @GetMapping("/stat/type")
    @SaCheckRole("1")
    public Result<?> statOpType(
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        
        // 这里可以添加统计逻辑
        return Result.success("统计功能开发中");
    }
}

